<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <!-- 角色管理 -->
        <el-tab-pane label="角色管理" lazy>
          <el-button
            type="primary"
            style="margin-top: 30px"
            @click="Add"
          >新增角色</el-button>
          <!-- 表单区域 -->
          <el-table :data="list">
            <el-table-column label="序号" type="index">
              <template v-slot="{ $index }">
                <div>{{ (page.page - 1) * page.pagesize + $index + 1 }}</div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button
                    type="text"
                    @click="openper(row.id)"
                  >分配权限</el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row.id)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 5, 100, 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy><Tnfo /></el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 弹出框 -->
    <Dialog ref="add" v-model="show" @getSysRoleAPI="getSysRoleAPI" />
    <!-- 分配权限组件 -->
    <PermissOnSet ref="Per" />
  </div>
</template>

<script>
import { sysRoleAPI, sysRoledeleteAPI } from '@/api/'
export default {
  components: {
    Tnfo: () => import('./components/info.vue'),
    Dialog: () => import('./components/add.vue'),
    PermissOnSet: () => import('./components/permissionSet.vue')
  },
  data() {
    return {
      list: [{}],
      page: {
        total: 100,
        page: 1,
        pagesize: 5
      },
      show: false
    }
  },
  created() {
    this.getSysRoleAPI()
  },
  methods: {
    // 分配权限点击事件
    openper(id) {
      this.$refs.Per.opSetPermissOn = true
      this.$refs.Per.getDateId(id)
    },
    // 页容量改变
    // size:每页显示多少数据的选项改变后传过来的 为当前选中的数字选项
    handleSizeChange(size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getSysRoleAPI()
      console.log(size)
    },
    // 页码改变
    // page 当前点击的页码 点击后传过来的那页
    handleCurrentChange(page) {
      this.page.page = page
      this.getSysRoleAPI()
      console.log(page)
    },
    // 获取员工列表
    async getSysRoleAPI() {
      const res = await sysRoleAPI(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
    },
    // 新增
    Add() {
      this.show = true
      this.$refs.add.mode = 'add'
    },
    // 删除
    del(id) {
      this.$confirm('是否删除此角色', '提示')
        .then(async() => {
          this.$message.success('删除成功')
          await sysRoledeleteAPI(id)
          this.page.page = 1
          this.getSysRoleAPI()
        })
        .catch(() => {})
    },
    edit(row) {
      this.show = true
      this.$refs.add.from = JSON.parse(JSON.stringify(row))
      this.$refs.add.mode = 'edit'
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 15px;
  .page {
    text-align: right;
    margin-top: 20px;
  }
}
</style>
